//
//  ViewController.m
//  TouchExplorer
//
//  Created by Justsoso on 16/3/17.
//  Copyright © 2016年 Justsoso. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
{
    BOOL bMove;
    CGPoint pointXY;
}
@property (weak, nonatomic) IBOutlet UILabel *label1;
@property (weak, nonatomic) IBOutlet UILabel *label2;
@property (weak, nonatomic) IBOutlet UILabel *label3;
@property (weak, nonatomic) IBOutlet UILabel *label4;

@property (nonatomic,strong) UIView *moveView;


@end

@implementation ViewController

// 1.修改当前代码，让moveView在你去拖动他的时候再跟着你移动，如果手指在它的范围之外移动，不要让他跟着移动
// 2.并且触摸时手指按在moveView的哪位置，移动时也就保持这个位置拖动，比如你一开始拖动的是moveView的左下角，移动时，保持这个偏移.

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.moveView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 50, 50)];
    self.moveView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:self.moveView];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
{
    // 1. 触摸事件
    self.label1.text = @"触摸开始";
    // 2. 触摸手指数量
    self.label2.text = [NSString stringWithFormat:@"数量:%lu",(unsigned long)touches.count];
    // 3. 触摸点坐标
    UITouch *touch = touches.anyObject;// 得到触摸对象
    CGPoint p = [touch locationInView:self.view];// 得到触摸点在self.view中的坐标
    self.label3.text = [NSString stringWithFormat:@"坐标%@",NSStringFromCGPoint(p)];
    // 4. 触摸连击数
    self.label4.text = [NSString stringWithFormat:@"连击次数:%lu",(unsigned long)touch.tapCount];
    
    bMove = CGRectContainsPoint(self.moveView.frame, p);
    
    if (bMove)
    {
        pointXY = CGPointMake(p.x - self.moveView.center.x, p.y-self.moveView.center.y);
    }


    
}


- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
{
    self.label1.text = @"触摸移动";
    if (bMove)
    {
        UITouch *touch = [touches anyObject];
        CGPoint point = [touch locationInView:self.view];
        
        self.moveView.center = CGPointMake(point.x - pointXY.x, point.y - pointXY.y);
    }
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
{
    self.label1.text = @"触摸结束";
    bMove = NO;
}
@end
